package com.hxd.mall.widget.verifycode;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.mall.R;

public class VerifyCodeView extends RelativeLayout {

    public EditText editText;
    private TextView[] textViews;
    private View[] spaceViews;
    private int codeLength = 6;
    private String inputContent;

    private static int MAX_LENGTH = 6;

    public VerifyCodeView(Context context) {
        this(context, null);
    }

    public VerifyCodeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerifyCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.view_verify_code, this);

        textViews = new TextView[MAX_LENGTH];
        textViews[0] = (TextView) findViewById(R.id.tv_0);
        textViews[1] = (TextView) findViewById(R.id.tv_1);
        textViews[2] = (TextView) findViewById(R.id.tv_2);
        textViews[3] = (TextView) findViewById(R.id.tv_3);
        textViews[4] = (TextView) findViewById(R.id.tv_4);
        textViews[5] = (TextView) findViewById(R.id.tv_5);

        spaceViews = new View[MAX_LENGTH - 1];
        spaceViews[0] = findViewById(R.id.space_0);
        spaceViews[1] = findViewById(R.id.space_1);
        spaceViews[2] = findViewById(R.id.space_2);
        spaceViews[3] = findViewById(R.id.space_3);
        spaceViews[4] = findViewById(R.id.space_4);

        editText = (EditText) findViewById(R.id.edit_text_view);
        editText.setCursorVisible(false);//隐藏光标
        setEditTextListener();
    }

    public void setCodeLength(int codeLength) {
        this.codeLength = codeLength;
        if (this.codeLength > MAX_LENGTH) {
            this.codeLength = MAX_LENGTH;
        }
        for (int i = 0; i < MAX_LENGTH; i++) {
            if (i < this.codeLength - 1) {
                textViews[i].setVisibility(VISIBLE);
                spaceViews[i].setVisibility(VISIBLE);
            } else {
                if (i == this.codeLength - 1) {
                    textViews[i].setVisibility(VISIBLE);
                } else {
                    textViews[i].setVisibility(GONE);
                }
                if (i < MAX_LENGTH - 1) {
                    spaceViews[i].setVisibility(GONE);
                }
            }
        }
    }

    private void setEditTextListener() {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                inputContent = editText.getText().toString();

                if (inputCompleteListener != null) {
                    if (inputContent.length() >= codeLength) {
                        inputCompleteListener.inputComplete();
                    } else {
                        inputCompleteListener.invalidContent();
                    }
                }

                for (int i = 0; i < codeLength; i++) {
                    if (i < inputContent.length()) {
                        textViews[i].setText(String.valueOf(inputContent.charAt(i)));
                        textViews[i].setBackgroundResource(R.drawable.ev_bottom_line_blue);
                    } else {
                        textViews[i].setText("");
                        textViews[i].setBackgroundResource(R.drawable.ev_bottom_line);
                    }
                }
            }
        });
    }


    private InputCompleteListener inputCompleteListener;

    public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
        this.inputCompleteListener = inputCompleteListener;
    }

    public interface InputCompleteListener {

        void inputComplete();

        void invalidContent();
    }

    public String getEditContent() {
        return inputContent;
    }

    public void clearInput() {
        for (int i = 0; i < codeLength; i++) {
            textViews[i].setText("");
            textViews[i].setBackgroundResource(R.drawable.ev_bottom_line);
        }
        editText.getText().clear();
    }

    public void setEtErrorTip(int backgroundId) {
        for (int i = 0; i < codeLength; i++) {
            textViews[i].setBackgroundResource(backgroundId);
        }
    }
}